package com.zahb.qadx.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseActivity;
import com.zahb.qadx.base.BaseApplication;
import com.zahb.qadx.model.ExamBean;
import com.zahb.qadx.model.ExamInfoBean;
import com.zahb.qadx.model.TaskListBean;
import com.zahb.qadx.net.RetrofitService;
import com.zahb.qadx.ui.adapter.ExamAdapter;
import com.zahb.qadx.util.Constant;
import com.zahb.qadx.util.Tips;
import com.zahb.qadx.util.WebViewHelper;
import com.zahb.qadx.webview.MyWebViewActivity;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;


public class MyExamActivity extends BaseActivity {

    @BindView(R.id.tvBack)
    AppCompatTextView tvBack;
    @BindView(R.id.tv_title)
    AppCompatTextView tvTitle;
    @BindView(R.id.tvDes)
    AppCompatTextView tvDes;
    @BindView(R.id.recyc)
    RecyclerView recyc;
    @BindView(R.id.tvStart)
    TextView tvStart;

    TaskListBean taskListBean ;

    ArrayList<ExamBean> list = new ArrayList<>();
    ExamAdapter examAdapter = new ExamAdapter(list);


    @Override
    protected int getLayout() {
        return R.layout.activity_my_exam;
    }

    @Override
    protected void initViews() {
        mUnbinder = ButterKnife.bind(this);
        taskListBean = getIntent().getParcelableExtra("data");

        recyc.setLayoutManager(new LinearLayoutManager(this));
        recyc.setAdapter(examAdapter);

        tvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        tvStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(examInfoBean==null){
                    return;
                }
                Date date = new Date();
                String nowTime =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
                if(nowTime.compareTo(examInfoBean.getStartTime())<0){
                    //ToastUtils.s(MyExamActivity.this,"考试暂未开始，请按规定时间参加考试");
                    showBindToast("考试暂未开始，请按规定时间参加考试");
                    return;
                }
                if(nowTime.compareTo(examInfoBean.getEndTime())>0){
                    //ToastUtils.s(MyExamActivity.this,"考试已经结束，无法参加考试");
                    showBindToast("考试已经结束，无法参加考试");
                    return;
                }
                if(examInfoBean.getExamTotalCount()<=examInfoBean.getExamCount()){
                    //ToastUtils.s(MyExamActivity.this,"您的考试次数已用完，暂时无法进行考试！");
                    showBindToast("您的考试次数已用完，暂时无法进行考试！");
                    return;
                }

                try {

                    String token = URLEncoder.encode(BaseApplication.getContext().getToken(), "UTF-8");
                    String url = WebViewHelper.getBaseUrl() + "exam/examInfo?sourceId=" + taskListBean.getBusinessId() + "&fromType=1&token=" + token;
                    Log.e("MyWebViewActivity", "url: " + url);
                    MyWebViewActivity.actionStart(getContext(), url, "", 1);
                    } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        });

        getTaskList();
    }

    @Override
    protected int getTitleStringRes() {
        return 0;
    }

    ExamInfoBean examInfoBean;

    private void getTaskList() {
        if(taskListBean==null){
            return;
        }
        addDisposable(RetrofitService.getNetService()
                .examinfo(taskListBean.getBusinessId())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                    if (response.getStatusCode() == Constant.CODE_SUCCESS) {
                        examInfoBean = response.getData();

                        tvTitle.setText(response.getData().getExamName());
                        tvDes.setText("总分 "+response.getData().getTotalScore()+" | 及格分 "+response.getData().getPassScore());

                        list.add(new ExamBean("考试信息"));
                        list.add(new ExamBean("开始时间",response.getData().getStartTime()));
                        list.add(new ExamBean("结束时间",response.getData().getEndTime()));
                        list.add(new ExamBean("开始时长",response.getData().getExamTime()+"分钟"));

                        list.add(new ExamBean("考试题型"));
                        for (ExamInfoBean.ExamTypeVOListEntity entity : response.getData().getExamTypeVOList()){
                            if(entity.getQuestionType() <= 0 || entity.getQuestionCount()<=0){
                                continue;
                            }
                            String name="单选题";
                            if(entity.getQuestionType() == 1){
                                name="单选题";
                            }
                            if(entity.getQuestionType() == 2){
                                name="多选题";
                            }
                            if(entity.getQuestionType() == 3){
                                name="判断题";
                            }
                            if(entity.getQuestionType() == 4){
                                name="填空题";
                            }
                            if(entity.getQuestionType() == 5){
                                name="简答题";
                            }

                            list.add(new ExamBean(name,"共"+entity.getQuestionCount()+"题",1));
                        }
                        examAdapter.notifyDataSetChanged();

                    }
                    else {
                        showBindToast(response.getErrorInfo());
                    }
                }, throwable -> {
                    Tips.RequestError(getActivity());
                    throwable.printStackTrace();

                }));
    }

}
